/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package researchgrants.utils;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.Window;
import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.border.TitledBorder;
import researchgrants.parts.LoggedData.HistoricallyLoggedData;
import researchgrants.parts.LoggedData.panels.LoggedDataPanels;
import javax.swing.event.ChangeListener;
import researchgrants.parts.panelsManager.LoggedDataPanelsManager;

/**
 *
 * @author DOStudent1
 */
public class MyDialog extends JDialog {
    
    public MyDialog(Window owner) {
        super(owner, ModalityType.APPLICATION_MODAL);
    }

    
    @Override
    protected JRootPane createRootPane() {
        JRootPane tmpRootPane=new MyRootPane();
        WindowHelpers.allowEscapeRootPane(this, tmpRootPane);

//        Dimension size=new Dimension();
//        size.height=GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height;
//        size.width=GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width;
//        this.setPreferredSize(size);

        return(tmpRootPane);
    }

    protected static JPanel wrapSection(JPanel sectionPanel, String title) {

        JPanel pnlWrapper=new JPanel(new BorderLayout(0, 0));
        TitledBorder titledBorder = BorderFactory.createTitledBorder(title);
        titledBorder.setTitlePosition(TitledBorder.TOP);
        pnlWrapper.setBorder(titledBorder);
        
        JPanel pnlContent=new JPanel(new BorderLayout(0, 0));
        pnlContent.add(sectionPanel, BorderLayout.CENTER);
        pnlWrapper.add(pnlContent);
        
        return(pnlWrapper);
                
    }

    
    protected static LoggedDataPanelsManager createLoggedDataPanelManager(boolean isMandatory, boolean isNew, LoggedDataPanels panels, Window parentWindow, HistoricallyLoggedData data, Container fieldContainer) {
        return(createLoggedDataPanelManager(isMandatory, isNew, panels, parentWindow, data, fieldContainer, null));
    }
    
    protected static LoggedDataPanelsManager createLoggedDataPanelManager(boolean isMandatory, boolean isNew, LoggedDataPanels panels, Window parentWindow, HistoricallyLoggedData data, Container fieldContainer, ChangeListener changeEventListener) {
        LoggedDataPanelsManager loggedDataPanelsManager =new LoggedDataPanelsManager();
        loggedDataPanelsManager.setMandatory(isMandatory);
        loggedDataPanelsManager.startAsNew(isNew);
        {
            panels.setParent(parentWindow, loggedDataPanelsManager);
            if (changeEventListener!=null) {
                panels.addChangeListener(changeEventListener);
            }
            if (!isNew) {
                panels.setData(data);
            }
            loggedDataPanelsManager.setPanels(panels);
        }
        fieldContainer.removeAll();
        fieldContainer.add(loggedDataPanelsManager);
        loggedDataPanelsManager.display();   
        return (loggedDataPanelsManager);
    }

    
    public static int saveLoggedDataPanelsManagerData(LoggedDataPanelsManager loggedDataPanelsManager) {
            ((LoggedDataPanelsManager)loggedDataPanelsManager).saveData();
            return(((LoggedDataPanels)(((LoggedDataPanelsManager)loggedDataPanelsManager).getPanels())).getData().getId());
    }
            
    
    private class MyRootPane extends JRootPane {

        JPanel pnlContentInnerMost;
        JPanel pnlContentOuterMost;
        JScrollPane scrollPane;

        @Override
        protected Container createContentPane() {
            pnlContentInnerMost=new JPanel(new BorderLayout(0, 0));
            pnlContentOuterMost=new JPanel(new BorderLayout(0, 0));
            scrollPane=new JScrollPane(pnlContentInnerMost, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            pnlContentOuterMost.add(scrollPane, BorderLayout.CENTER);
            return super.createContentPane();
        }

        @Override
        public void setContentPane(Container content) {
            pnlContentInnerMost.add(content, BorderLayout.CENTER);
            super.setContentPane(pnlContentOuterMost);
        }

        @Override
        public Container getContentPane() {
            return(pnlContentInnerMost);
        }

        
    }
    
    public static void changeToTitleLabel(JLabel label) {
        label.setFont(new Font(label.getFont().getName(), Font.BOLD, label.getFont().getSize()+4));
        
    }

}
